//
//  ViewController.m
//  tomspak
//
//  Created by song on 15/1/5.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)drink:(UIButton *)sender {
    
    
    [self runAnimationWithName:@"drink" andCount:54];
    
}

- (IBAction)eat:(UIButton *)sender {
    
    [self runAnimationWithName:@"eat" andCount:40];
    
}

-(void) runAnimationWithName:(NSString *)name andCount:(int)count
{
    if([self.tom isAnimating]) return;
    
    NSMutableArray *imagesdatas = [[NSMutableArray  alloc]initWithCapacity:0];
    
    for (int i= 0;i< count; i++) {
        
        NSString *filename = [NSString stringWithFormat:@"%@_%02d.jpg",name,i];
        NSBundle *mainbundle =[NSBundle mainBundle];
        NSString *path = [mainbundle pathForResource:filename ofType:nil];
        UIImage *image = [UIImage imageWithContentsOfFile:path];
//        UIImage *image = [UIImage imageNamed:[filename];
        [imagesdatas addObject:image];
        
    }
    // 播放的动画 的图片集合
    self.tom.animationImages = imagesdatas;
    // 代表这个动画播放的次数 ，1 代表只播放一次
    self.tom.animationRepeatCount =1;
    
    self.tom.animationDuration = imagesdatas.count*0.1;
    
    // 开始播放动画
    [self.tom startAnimating];
}

- (IBAction)scratch:(UIButton *)sender {
    
    [self runAnimationWithName:@"scratch" andCount:56];
    
}
@end
